package com.lin.blog.controller;


import com.lin.blog.entity.User;
import com.lin.blog.entity.UserFrom;
import com.lin.blog.service.IUserService;
import com.lin.blog.valid.UserUpdateGroup;
import com.lin.constant.JwtConstant;
import com.lin.constant.RedisConstant;
import com.lin.shiro.utils.JwtUtil;
import com.lin.shiro.utils.SHA256Util;
import com.lin.utils.RedisUtils;
import com.lin.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Api(tags = "User接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    private static Logger logger = LoggerFactory.getLogger(UserController.class);


    @ApiOperation("更改昵称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID",required = true),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", required = true)})
    @PostMapping("/nickname/{id}")
    public Result setNickName(@PathVariable("id") Long userId, @PathVariable("nickname") String nickName){
       return userService.setNickName(userId,nickName);
    }


    @ApiOperation("更新头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名",required = true),
            @ApiImplicitParam(name = "headImageUrl", value = "用户头像URL", required = true)})
    @PutMapping("/head_image")
    public Result updateUserHeadImage(@RequestParam(name = "username")String username, @RequestParam(name = "headImageUrl")String headImageUrl){
        return userService.updateUserHeadImage(username,headImageUrl);
    }

    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名",required = true),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true)})
    @PutMapping("/update_password")
    public Result updateUserPasswordByUserName(@RequestParam(name = "username") String username,
                                               @RequestParam(name = "oldPassword") String oldPassword,
                                               @RequestParam(name = "newPassword") String newPassword){
        return userService.updateUserPasswordByUserName(username,oldPassword,newPassword);
    };


    /**
     * 通过token获取用户信息
     * @param token
     * @return
     */
    @ApiOperation("通过token获取用户信息")
    @ApiImplicitParam(name = "token", value = "token",required = true)
    @GetMapping("/info")
    public Result getByToken(String token){

        return userService.getUserInfoByToken(token);
    }

    /**
     * 通过主键ID查询User
     * @param id
     * @return
     */
    @ApiOperation("通过主键ID查询用User")
    @ApiImplicitParam(name = "id", value = "用户ID", defaultValue = "0",required = true)
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable(value = "id",required = true) Long id){
        return userService.getUser(id);
    }

    /**
     * 插入一条User记录
     * @param user
     * @return
     */
    @ApiOperation("插入一条User记录")
    @PostMapping("/register")
    public Result insertUser(@RequestBody @Valid User user) {
        return userService.saveUser(user);
    }



    /**
     * 通过主键ID删除User
     * @param id
     * @return
     */
    @RequiresRoles("ADMIN")
    @ApiOperation("通过主键ID删除User")
    @ApiImplicitParam(name = "id", value = "用户ID", defaultValue = "0",required = true)
    @DeleteMapping("/{id}")
    public Result deleteUserById(@PathVariable(value = "id",required = true) Long id){
        return userService.removeUser(id);
    }

    /**
     * 通过主键ID更新User
     * @param id
     * @param user
     * @return
     */
    @ApiOperation("通过主键ID更新User")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @PutMapping("/{id}")
    public Result updateUser(@PathVariable(value = "id",required = true) Long id,@RequestBody @Validated(UserUpdateGroup.class) User user){
        return userService.updateUser(id,user);
    }



    /**
     * 获取UserList
     * @return
     */
    @ApiOperation("获取UserList")
    @GetMapping("/list")
    public Result  getUserList(){
        return userService.listUser();
    }


    /**
     * 分页获取UserList
     * @return
     */
    @ApiOperation("分页获取UserList")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "pageNum", value = "第N页"),
    @ApiImplicitParam(name = "pageSize", value = "每页N条", defaultValue = "20")})
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result  getUserList(@PathVariable("pageNum")  Long pageNum,
                               @PathVariable("pageSize") @RequestParam(value = "pageSize",defaultValue = "20") Long pageSize){
        return userService.listUser(pageNum,pageSize);

    }

}
